//
//  Register.m
//  ShoppingCompare
//
//  Created by Wenjie on 11-6-1.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Register.h"
#import "LKGoogleTranslator.h"
#import "RootViewController.h"
#import "ChangeFrom.h"
#import "ChangeTo.h"

@implementation Register

- (void)viewDidLoad {
	[super viewDidLoad];
	self.title = @"Register Key";
	self.view.backgroundColor = [UIColor whiteColor];
	
	r_area1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	r_area1.frame = CGRectMake(35, 30, 80, 40);
	[r_area1 addTarget:self action:@selector(change1) forControlEvents:UIControlEventTouchUpInside];
	[r_area1 setTitle:@"China" forState:UIControlStateNormal];
	r_area1.backgroundColor = [UIColor whiteColor];
	
	r_area2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	r_area2.frame = CGRectMake(205, 30, 80, 40);
	[r_area2 addTarget:self action:@selector(change2) forControlEvents:UIControlEventTouchUpInside];		
	[r_area2 setTitle:@"Japan" forState:UIControlStateNormal];
	r_area2.backgroundColor = [UIColor whiteColor];
		
	r_key1 = [[UITextView alloc] initWithFrame:CGRectMake(30, 80, 260, 80)];
	r_key1.backgroundColor = [UIColor colorWithRed:0.88f green:0.93f blue:0.93f alpha:1.0f];
	[r_key1 setFont:[UIFont systemFontOfSize:17.0]];
	r_key1.autocorrectionType = UITextAutocorrectionTypeNo;
	r_key1.delegate = self;
		
	r_key2 = [[UITextView alloc] initWithFrame:CGRectMake(30, 220, 260, 80)];
	r_key2.backgroundColor = [UIColor colorWithRed:0.88f green:0.93f blue:0.93f alpha:1.0f];
	[r_key2 setFont:[UIFont systemFontOfSize:17.0]];
	r_key2.autocorrectionType = UITextAutocorrectionTypeNo;
	r_key2.delegate = self;
		
	r_trans = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	r_trans.frame = CGRectMake(170, 170, 120, 40);
	[r_trans addTarget:self action:@selector(translate) forControlEvents:UIControlEventTouchUpInside];		
	[r_trans setTitle:@"Translation" forState:UIControlStateNormal];
	r_trans.backgroundColor = [UIColor whiteColor];
		
	r_register = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	r_register.frame = CGRectMake(170, 310, 120, 40);
	[r_register addTarget:self action:@selector(regi) forControlEvents:UIControlEventTouchUpInside];		
	[r_register setTitle:@"Register" forState:UIControlStateNormal];
	r_register.backgroundColor = [UIColor whiteColor];
		
	[self.view addSubview:r_area1];
	[self.view addSubview:r_key1];
	[self.view addSubview:r_area2];
	[self.view addSubview:r_key2];
	[self.view addSubview:r_trans];
	[self.view addSubview:r_register];
		
	UIImageView* r_image = [[[UIImageView alloc] initWithImage:
							([UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"gif"]])] autorelease];
	r_image.frame = CGRectMake(120.0f, 30.0f, 80.0f, 40.0f);
	[self.view addSubview:r_image];
}

- (void) translate {
	LKGoogleTranslator* translator = [[[LKGoogleTranslator alloc] init] autorelease];
	if (r_key1.text.length > 0) {
		if ([r_area1.titleLabel.text isEqualToString:@"China"] ) {
			r_key2.text = [translator translateText: r_key1.text fromLanguage: kLKLanguageChinese toLanguage: kLKLanguageJapanese];
		} else {
			r_key2.text = [translator translateText: r_key1.text fromLanguage: kLKLanguageJapanese toLanguage: kLKLanguageChinese];
		}
	} else {
		if (r_key2.text.length > 0) {
			if ([r_area1.titleLabel.text isEqualToString:@"China"] ) {
				r_key1.text = [translator translateText: r_key2.text fromLanguage: kLKLanguageJapanese toLanguage: kLKLanguageChinese];
			} else {
				r_key1.text = [translator translateText: r_key2.text fromLanguage: kLKLanguageChinese toLanguage: kLKLanguageJapanese];
			}
		}
	}
}

- (void) regi {
	if (r_key1.text.length > 0 && r_key2.text.length > 0) {
		if ([r_area1.titleLabel.text isEqualToString:@"China"]) {
			[Register setChina:r_key1.text andJapan:r_key2.text];
		} else {
			[Register setChina:r_key2.text andJapan:r_key1.text];
		}
	}
}

+ (void) setChina:(NSString*) china andJapan:(NSString*) japan {
	NSString *documentsPath = [self getDocumentsDirectory];
	NSString *fullPath = [documentsPath stringByAppendingPathComponent:@"registerC.plist"];
	NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath];
	NSString* fullPath2 = [documentsPath stringByAppendingPathComponent:@"registerJ.plist"];
	NSMutableDictionary* plistDict2 = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath2];
	if (plistDict != nil && plistDict2 != nil) {
		[plistDict removeObjectForKey:(NSString*) china];
		[plistDict2 removeObjectForKey:(NSString*) japan];
		[plistDict setObject:(NSString*) japan forKey:(NSString*) china];
		[plistDict2 setObject:(NSString*) china forKey:(NSString*) japan];
		[plistDict writeToFile:fullPath atomically:YES];
		[plistDict2 writeToFile:fullPath2 atomically:YES];
	}
	[plistDict release];
	[plistDict2 release];
}
+ (NSString*)getDocumentsDirectory {  
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
	return [paths objectAtIndex:0];  
} 
+ (NSString*)getKeyFromChina:(NSString *)sourceKey {
	NSString *documentsPath = [self getDocumentsDirectory];
	NSString *fullPath = [documentsPath stringByAppendingPathComponent:@"registerC.plist"];
	NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath];
	if (plistDict != nil) {
		NSString* result = [NSString stringWithFormat:@"%@",[plistDict objectForKey:sourceKey]];
		[plistDict release];
		return result;
	}
	return nil;
}
+ (NSString*)getKeyFromJapan:(NSString *)sourceKey {
	NSString *documentsPath = [self getDocumentsDirectory];
	NSString *fullPath = [documentsPath stringByAppendingPathComponent:@"registerJ.plist"];
	NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath];
	if (plistDict != nil) {
		NSString* result = [NSString stringWithFormat:@"%@",[plistDict objectForKey:sourceKey]];
		[plistDict release];
		return result;
	}
	return nil;
}

- (void) change1 {
	RootViewController* rvc = (RootViewController*) [[UIApplication sharedApplication] delegate];
	UINavigationController* navi = [rvc->search navigationController];
	ChangeFrom* chF = [[ChangeFrom alloc] init];
	[navi pushViewController:chF animated:YES];
}

- (void) change2 {
	RootViewController* rvc = (RootViewController*) [[UIApplication sharedApplication] delegate];
	UINavigationController* navi = [rvc->search navigationController];
	ChangeTo* chT = [[ChangeTo alloc] init];
	[navi pushViewController:chT animated:YES];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated {
	if (isAdv) {
		[r_area1 setTitle:@"China" forState:UIControlStateNormal];
		[r_area2 setTitle:@"Japan" forState:UIControlStateNormal];
	} else {
		[r_area1 setTitle:@"Japan" forState:UIControlStateNormal];
		[r_area2 setTitle:@"China" forState:UIControlStateNormal];
	}
}

// --------------------------------------
- (void) textViewDidBeginEditing:(UITextView *)textView {
	self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone
																			  target:self action:@selector(doneEditing:)]
											  autorelease];
	r_area1.enabled = NO;
	r_area2.enabled = NO;
	r_trans.enabled = NO;
	r_register.enabled = NO;
	if (textView.frame.origin.y > 200) {
		r_key1.editable = NO;
		[UIView beginAnimations:nil context:nil];
		self.view.center = CGPointMake(160, 100);
		[UIView commitAnimations];
	} else {
		r_key2.editable = NO;
	}
}

// --------------------------------------
- (void) doneEditing:(id) sender {
	[r_key1 resignFirstResponder];
	[r_key2 resignFirstResponder];
	self.navigationItem.rightBarButtonItem = NULL;
	if (self.view.center.y < 200) {
		[UIView beginAnimations:nil context:nil];
		self.view.center = CGPointMake(160, 208);
		[UIView commitAnimations];
	}
	r_area1.enabled = YES;
	r_area2.enabled = YES;
	r_trans.enabled = YES;
	r_register.enabled = YES;
	r_key1.editable = YES;
	r_key2.editable = YES;
}

@end